<?php
    include_once('../../src/classes/class.mysql.php');
    
    session_start();
    
    $comtec_id_mainteiner = $_GET["id"];
    $comtec_id_user       = $_SESSION['comtec_id_user'];
    
    $op                   = isset($_GET["op"])? $_GET["op"] : "";
    
    $db = new MySQL();
    
    if($_SESSION["users"]!="clients"){
     $tsql = "SELECT o.company_name,m.comtec_id_machine, m.cod_mainteiner, m.brand, m.model, m.year, 
                    DATE_FORMAT(m.date_next_maintenance,'%d/%m/%Y') AS date,CONCAT_WS(' ',r.name,r.lastname1,r.lastname2) AS encargado ";  
     $tsql .= " FROM (machines as m INNER JOIN owner as o ON m.comtec_id_owner = o.comtec_id_owner) LEFT JOIN 
                responsible as r ON m.comtec_id_responsible = r.comtec_id_responsible
                WHERE m.comtec_id_mainteiner=$comtec_id_mainteiner and m.status_reg=1 ";
    }else{
          $tsql = "SELECT o.company_name,m.comtec_id_machine, m.cod_mainteiner, m.brand, m.model, m.year, 
                    DATE_FORMAT(m.date_next_maintenance,'%d/%m/%Y') AS date,CONCAT_WS(' ',r.name,r.lastname1,r.lastname2) AS encargado ";  
          $tsql .= " FROM (machines as m INNER JOIN owner as o ON m.comtec_id_owner = o.comtec_id_owner) LEFT JOIN 
                responsible as r ON m.comtec_id_responsible = r.comtec_id_responsible
                WHERE o.comtec_id_user=$comtec_id_user AND m.comtec_id_mainteiner=$comtec_id_mainteiner and m.status_reg=1 ";
         }
    if($op == ""){
      $tsql .= " ORDER BY m.brand ASC, m.date_next_maintenance DESC ";  
      $op = 1;  
    }elseif($op == "1"){
      $tsql .= " ORDER BY cod_mainteiner ASC ";  
      $op = 2;
    }elseif($op == "2"){
      $tsql .= " ORDER BY cod_mainteiner DESC ";
      $op = 1;
    }     
    $stmt = $db->query($tsql);       
    
    $texto = "<table class='table'>";     
    if(($_SESSION["users"]=="managers") || ($_SESSION["users"]=="administratives-1")){     
     $texto .= "<tr><th colspan=2></th>";
    }else{ 
          if ($_SESSION["users"]!="clients"){
           $texto .= "<tr><th></th>";
          }
          }                
    $texto .= "<th><a href='javascript:ordenarMaquinas(".$op.");'>C&oacute;digo</a></th>
               <th>Marca</th>
               <th>Modelo</th>
               <th>A&ntilde;o</th>
               <th>Propietario</th>
               <th>Encargado</th>
               <th>Fecha pr&oacute;x. oper.</th>             
               </tr>";  
    $cont = 0;
    
    while ($row = mysql_fetch_array($stmt, MYSQL_ASSOC)){
     $cont += 1; 
     $comtec_id_machine=$row['comtec_id_machine'];   
     $cod_mainteiner=$row['cod_mainteiner'];
     $brand=$row['brand'];
     $model=$row['model'];
     $year=$row['year'];
     $date=$row['date'];
     $company_name=$row['company_name'];
     $encargado=$row['encargado'];
     $texto .= "<tr>";
     
     $url_edit='http://'.$_SERVER['HTTP_HOST'].'/iconos/edit.png';
     if($_SESSION["users"]!="clients"){
     $texto .= "<td><a href='javascript:loadMachine(".$comtec_id_machine.");'><img src='$url_edit' alt='editar' style='border: 0px;' /></a></td>";}
     $url_delete='http://'.$_SERVER['HTTP_HOST'].'/iconos/delete.png';
     if($_SESSION["users"]=="managers"){
     $texto .= "<td><a href='javascript:deleteMachine(".$comtec_id_machine.");'><img src='$url_delete' alt='eliminar' style='border: 0px;' /></a></td>";}
     $texto .= "<td><a href='javascript:machine(".$comtec_id_machine.");'>$cod_mainteiner</a></td>";
     $texto .= "<td>$brand</td>";
     $texto .= "<td>$model</td>";
     $texto .= "<td>$year</td>";
     $texto .= "<td>$company_name</td>";
     $texto .= "<td>$encargado</td>";
     $texto .= "<td>$date</td>";
     $texto .= "</tr>";
    }
    $texto .= "</table>";
    $texto .= '<div id="result"></div>';
    echo "<p class='total'>Total: $cont</p>".$texto;
?>